<?php


namespace App\Http\Controllers\Api\V1;


use App\Http\Resources\CommentCollection;
use App\Http\Resources\DocArticleCollection;
use App\Logic\UserBuyLogic;
use App\Models\Comment;
use App\Models\Doc;
use App\Models\DocArticle;
use App\Models\DocPage;
use App\Models\UserListened;

class ArticleController extends BaseController
{

    /**
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function index()
    {
        $id = $this->getId();
        $user = $this->getUser();
        $doc_model = Doc::query()->withAnyStatus()->with([
            'category',
            'last_article',
            'listened' => function ($query) {
                $query->groupBy("user_id", "parent_id");
            },
            'article_listened',
            'user',
            'user.wx_user',
            'articles',
        ]);
        if ($user) {
            $doc_model->where("id", $id);
            $doc_model->where("status", 1);
            $doc_model->orWhere(function ($query) use ($user, $id) {
                $query->where("id", $id)->where("user_id", $user->id);
            });
        } else {
            $doc_model->where("id", $id);
            $doc_model->where("status", 1);
        }


        $doc = $doc_model->firstOrFail();
        $data['doc'] = \App\Http\Resources\Doc::make($doc);

        $comment = Comment::query()->with([
            'user',
            'user.wx_user'
        ])->orderByDesc('stick')->orderByDesc('id');
        $comment->where("commentable_id", $doc->id);
        $comment->where("commentable_type", Doc::class);
        $comment->where("is_hot", 1);
        $page = $comment->paginate();
        $data['doc_comment'] = CommentCollection::make($page);

        $doc->increment("view");
        return $this->success($data);
    }

    /**
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function menu()
    {
        $id = $this->getId();
        $doc = Doc::withAnyStatus()->with(['pages', 'user'])->findOrFail($id);
        $data['doc'] = \App\Http\Resources\Doc::make($doc)->show(['buy_info', 'doc_type', 'id', 'title']);
        $doc_article = DocArticle::query()->where('doc_id', $doc->id)->orderByDesc('order');
        $page = $doc_article->paginate();
        $data['menu'] = DocArticleCollection::make($page);
        return $this->success($data);
    }

    /**
     * @return mixed
     * @throws \App\Exceptions\ApiException
     */
    public function page_info()
    {
        $id = $this->getId();
        $user = $this->getUser();
        $page = DocArticle::query()->with([
            'user',
            'user.docs',
            'likers'
        ])->find($id);

        if (empty($page)) {
            $data['no_access'] = [
                'title' => '文章不存在'
            ];
            return $this->success($data);
        }
        $data = [];
        if ($page->doc->doc_type == 1 && $page->is_try == 0) {
            if ($user) {
                $userBuy = UserBuyLogic::judgeDoc($page->doc, $user);
                if ($userBuy->getIsShow()) {
                    $data['page'] = \App\Http\Resources\DocPage::make($page)->relate([
                        'doc' => ['show', 'id', 'title', 'buy_info']
                    ]);
                    $data['doc_menu'] = DocPage::getAllPage($page->doc);
                } else {
                    $data['no_access'] = [
                        'title' => '此文档为付费文档，请购买后查看'
                    ];
                }
            } else {
                $data['no_access'] = [
                    'title' => '此文档为付费文档，请登录后查看'
                ];
            }
        } else {
            $data['article'] = \App\Http\Resources\DocArticle::make($page)->relate([
                'doc' => ['show', 'id', 'title', 'buy_info'],
                'user' => ['show', 'id', 'avatar', 'name', 'is_follow', 'doc_num', 'fans']
            ]);
        }
        if ($user) {
            UserListened::addListened($page, $user);
        }

        return $this->success($data);
    }
}